home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsfcmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.3 KB  |  190 lines

  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsfcmap.c,v 1.8.2.2 2000/10/26 12:45:11 igorm Exp $ */
  20. /* CMap character decoding */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gxfcmap.h"
  26.  
  27. /* GC descriptors */
  28. public_st_cmap();
  29. /* Because lookup ranges can be elements of arrays, */
  30. /* their enum_ptrs procedure must never return 0 prematurely. */
  31. private 
  32. ENUM_PTRS_WITH(code_lookup_range_enum_ptrs,
  33.                gx_code_lookup_range_t *pclr) return 0;
  34. case 0:
  35.     if (pclr->value_type == CODE_VALUE_GLYPH) {
  36.         const byte *pv = pclr->values.data;
  37.         int k;
  38.  
  39.         for (k = 0; k < pclr->num_keys; ++k) {
  40.             gs_glyph glyph = 0;
  41.             int i;
  42.  
  43.             for (i = 0; i < pclr->value_size; ++i)
  44.                 glyph = (glyph << 8) + *pv++;
  45.             pclr->cmap->mark_glyph(glyph, pclr->cmap->mark_glyph_data);
  46.         }
  47.     }
  48.     return ENUM_OBJ(pclr->cmap);
  49. case 1: return ENUM_STRING(&pclr->keys);
  50. case 2: return ENUM_STRING(&pclr->values);
  51. ENUM_PTRS_END
  52. private
  53. RELOC_PTRS_WITH(code_lookup_range_reloc_ptrs, gx_code_lookup_range_t *pclr)
  54.     RELOC_VAR(pclr->cmap);
  55.     RELOC_STRING_VAR(pclr->keys);
  56.     RELOC_STRING_VAR(pclr->values);
  57. RELOC_PTRS_END
  58. public_st_code_lookup_range();
  59. public_st_code_lookup_range_element();
  60.  
  61. /* ---------------- Procedures ---------------- */
  62.  
  63. /*
  64.  * Initialize a just-allocated CMap, to ensure that all pointers are clean
  65.  * for the GC.
  66.  */
  67. void
  68. gs_cmap_init(gs_cmap_t *pcmap)
  69. {
  70.     memset(pcmap, 0, sizeof(*pcmap));
  71.     uid_set_invalid(&pcmap->uid);
  72. }
  73.  
  74. /* Get a big-endian integer. */
  75. private uint
  76. bytes2int(const byte *p, int n)
  77. {
  78.     uint v = 0;
  79.     int i;
  80.  
  81.     for (i = 0; i < n; ++i)
  82.         v = (v << 8) + p[i];
  83.     return v;
  84. }
  85.  
  86. /*
  87.  * Decode a character from a string using a code map, updating the index.
  88.  * Return 0 for a CID or name, N > 0 for a character code where N is the
  89.  * number of bytes in the code, or an error.  Shift the decoded bytes into
  90.  * *pchr.  For undefined characters, set *pglyph = gs_no_glyph and return 0.
  91.  */
  92. private int
  93. code_map_decode_next(const gx_code_map_t * pcmap, const gs_const_string * pstr,
  94.                      uint * pindex, uint * pfidx,
  95.                      gs_char * pchr, gs_glyph * pglyph)
  96. {
  97.     const byte *str = pstr->data + *pindex;
  98.     uint ssize = pstr->size - *pindex;
  99.     /*
  100.      * The keys are not sorted due to 'usecmap'.  Possible optimization :
  101.      * merge and sort keys in 'zbuildcmap', then use binary search here.
  102.      * This would be valuable for UniJIS-UTF8-H, which contains about 7000
  103.      * keys.
  104.      */
  105.     int i;
  106.  
  107.     for (i = pcmap->num_lookup - 1; i >= 0; --i) { /* reverse scan order due to 'usecmap' */
  108.         const gx_code_lookup_range_t *pclr = &pcmap->lookup[i];
  109.         int pre_size = pclr->key_prefix_size, key_size = pclr->key_size,
  110.             chr_size = pre_size + key_size;
  111.  
  112.         if (ssize < chr_size)
  113.             continue;
  114.         if (memcmp(str, pclr->key_prefix, pre_size))
  115.             continue;
  116.         /* Search the lookup range. We could use binary search. */
  117.         {
  118.             const byte *key = pclr->keys.data;
  119.             int step = key_size;
  120.             int k;
  121.             const byte *pvalue;
  122.  
  123.             if (pclr->key_is_range) {
  124.                 step <<= 1;
  125.                 for (k = 0; k < pclr->num_keys; ++k, key += step)
  126.                     if (memcmp(str + pre_size, key, key_size) >= 0 &&
  127.                         memcmp(str + pre_size, key + key_size, key_size) <= 0)
  128.                         break;
  129.             } else {
  130.                 for (k = 0; k < pclr->num_keys; ++k, key += step)
  131.                     if (!memcmp(str + pre_size, key, key_size))
  132.                         break;
  133.             }
  134.             if (k == pclr->num_keys)
  135.                 continue;
  136.             /* We have a match.  Return the result. */
  137.             *pchr = (*pchr << (chr_size * 8)) + bytes2int(str, chr_size);
  138.             *pindex += chr_size;
  139.             *pfidx = pclr->font_index;
  140.             pvalue = pclr->values.data + k * pclr->value_size;
  141.             switch (pclr->value_type) {
  142.             case CODE_VALUE_CID:
  143.                 *pglyph = gs_min_cid_glyph +
  144.                     bytes2int(pvalue, pclr->value_size) +
  145.                     bytes2int(str + pre_size, key_size) -
  146.                     bytes2int(key, key_size);
  147.                 return 0;
  148.             case CODE_VALUE_GLYPH:
  149.                 *pglyph = bytes2int(pvalue, pclr->value_size);
  150.                 return 0;
  151.             case CODE_VALUE_CHARS:
  152.                 *pglyph =
  153.                     bytes2int(pvalue, pclr->value_size) +
  154.                     bytes2int(str + pre_size, key_size) -
  155.                     bytes2int(key, key_size);
  156.                 return pclr->value_size;
  157.             default:            /* shouldn't happen */
  158.                 return_error(gs_error_rangecheck);
  159.             }
  160.         }
  161.     }
  162.     /* No mapping. */
  163.     *pglyph = gs_no_glyph;
  164.     return 0;
  165. }
  166.  
  167. /*
  168.  * Decode a character from a string using a CMap.
  169.  * Return like code_map_decode_next.
  170.  */
  171. int
  172. gs_cmap_decode_next(const gs_cmap_t * pcmap, const gs_const_string * pstr,
  173.                     uint * pindex, uint * pfidx,
  174.                     gs_char * pchr, gs_glyph * pglyph)
  175. {
  176.     uint save_index = *pindex;
  177.     int code;
  178.  
  179.     *pchr = 0;
  180.     code =
  181.         code_map_decode_next(&pcmap->def, pstr, pindex, pfidx, pchr, pglyph);
  182.     if (code != 0 || *pglyph != gs_no_glyph)
  183.         return code;
  184.     /* This is an undefined character.  Use the notdef map. */
  185.     *pindex = save_index;
  186.     *pchr = 0;
  187.     return code_map_decode_next(&pcmap->notdef, pstr, pindex, pfidx,
  188.                                 pchr, pglyph);
  189. }
  190.